/**
 * 封装的异步请求处理函数
 * 使用方法为:
 * request('接口名称',{key:value},'请求方式(默认为GET)')
 * .then(res=>{console.log(res)})
 */

import {
	setTimeout
} from "timers";
// let baseUrl = 'https://htg.wx.ahitalgroup.com/';
// let baseUrl = 'http://192.168.1.89:5858/';
// let baseUrl = 'http://192.168.1.86:20000/';
let baseUrl = 'http://192.168.1.201:8007/';

uni.setStorageSync('baseUrl', baseUrl)
export default async function request(mehtod, params, type, callBack) {
	//创建一个名为request请求的方法函数
	if (!type) {
		type = 'POST';
	}
	// let header = {
	//   //设置请求头信息

	//   'Authorization': getToken(),
	//   'X-Requested-With': 'XMLHttpRequest',
	//   "Accept": "application/json",
	//   "Content-Type": "application/json; charset=UTF-8"
	// };
	let http = {
		url: baseUrl + mehtod,
		data: params,
		method: type,
		header: {
			Authorization: uni.getStorageSync('token'),
		}
	};
	let promise = new Promise((resolve, reject) => {
		// uni.showLoading({
		// 	title: '加载中'
		// });
		uni.request(http).then(res => {
			let newdata = res[1].data.code; // if (newdata.code == 403) {
			if (newdata == 0) {
				// uni.hideLoading();
				// uni.showToast({
				//   title: res[1].data.msg,
				//   icon: 'none'
				// });
			} else {
				if (newdata == 2) {
					uni.removeStorageSync('token')
					uni.navigateTo({
						url: '../../pages/Login/Login'
					})
				} else if (newdata == 1) {
					// setTimeout(()=>{
					//  uni.navigateBack({
					//  	delta:1
					//  })
					// },1000)

				}
			}
			resolve(res[1].data);
		}).catch(err => {
			reject(err);
			console.log(err);
		});
	});
	return promise;
}
// export default request;